package com.project.util;


import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;


import java.util.Date;
import java.util.HashMap;

public class CreateToken {
    private  static final  long EXPIRE_TIME=1000*60*60;
    public static  final String TOKEN_SECRET="13cd761e-8805-4294-8641-fc28fc3120a7";
    public static String getToken(String json) {

        //过期时间
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        //设置头信息
        HashMap<String, Object> header = new HashMap<>(2);
        header.put("typ", "JWT");
        header.put("alg", "HS256");
        //附带username和userID生成签名
        String token= JWT.create().withHeader(header)
                .withClaim("json",json)
                .withExpiresAt(date).sign(algorithm);
        return token;
    }

    public static void main(String[] args) {
//        UserDto u = new UserDto();
//        u.setUserName("赵云");
//        u.setAge(20);

//        System.out.println(UUID.randomUUID().toString()+"UUID");

//       //转化成json
//      String json = JSON.toJSONString(u);
//      //json加密后的
//        System.out.println(CreateToken.getToken(json));
    }

}